<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="所属作物类型id" prop="cropTypeId">
      <el-input v-model="dataForm.cropTypeId" placeholder="所属作物类型id"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="别名" prop="otherName">
      <el-input v-model="dataForm.otherName" placeholder="别名"></el-input>
    </el-form-item>
    <el-form-item label="分布地区" prop="distriArea">
      <el-input v-model="dataForm.distriArea" placeholder="分布地区"></el-input>
    </el-form-item>
    <el-form-item label="形态特征" prop="shape">
      <el-input v-model="dataForm.shape" placeholder="形态特征"></el-input>
    </el-form-item>
    <el-form-item label="生活习性" prop="lifeHabbit">
      <el-input v-model="dataForm.lifeHabbit" placeholder="生活习性"></el-input>
    </el-form-item>
    <el-form-item label="防止手段" prop="controlMeasure">
      <el-input v-model="dataForm.controlMeasure" placeholder="防止手段"></el-input>
    </el-form-item>
    <el-form-item label="病害：0，虫害：1" prop="type">
      <el-input v-model="dataForm.type" placeholder="病害：0，虫害：1"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="creator">
      <el-input v-model="dataForm.creator" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updater">
      <el-input v-model="dataForm.updater" placeholder="更新者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateDate">
      <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          cropTypeId: '',
          name: '',
          otherName: '',
          distriArea: '',
          shape: '',
          lifeHabbit: '',
          controlMeasure: '',
          type: '',
          creator: '',
          createDate: '',
          updater: '',
          updateDate: ''
        },
        dataRule: {
          cropTypeId: [
            { required: true, message: '所属作物类型id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          otherName: [
            { required: true, message: '别名不能为空', trigger: 'blur' }
          ],
          distriArea: [
            { required: true, message: '分布地区不能为空', trigger: 'blur' }
          ],
          shape: [
            { required: true, message: '形态特征不能为空', trigger: 'blur' }
          ],
          lifeHabbit: [
            { required: true, message: '生活习性不能为空', trigger: 'blur' }
          ],
          controlMeasure: [
            { required: true, message: '防止手段不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '病害：0，虫害：1不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updater: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          updateDate: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/pest/croppestdisease/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.cropTypeId = data.cropPestDisease.cropTypeId
                this.dataForm.name = data.cropPestDisease.name
                this.dataForm.otherName = data.cropPestDisease.otherName
                this.dataForm.distriArea = data.cropPestDisease.distriArea
                this.dataForm.shape = data.cropPestDisease.shape
                this.dataForm.lifeHabbit = data.cropPestDisease.lifeHabbit
                this.dataForm.controlMeasure = data.cropPestDisease.controlMeasure
                this.dataForm.type = data.cropPestDisease.type
                this.dataForm.creator = data.cropPestDisease.creator
                this.dataForm.createDate = data.cropPestDisease.createDate
                this.dataForm.updater = data.cropPestDisease.updater
                this.dataForm.updateDate = data.cropPestDisease.updateDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/pest/croppestdisease/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'cropTypeId': this.dataForm.cropTypeId,
                'name': this.dataForm.name,
                'otherName': this.dataForm.otherName,
                'distriArea': this.dataForm.distriArea,
                'shape': this.dataForm.shape,
                'lifeHabbit': this.dataForm.lifeHabbit,
                'controlMeasure': this.dataForm.controlMeasure,
                'type': this.dataForm.type,
                'creator': this.dataForm.creator,
                'createDate': this.dataForm.createDate,
                'updater': this.dataForm.updater,
                'updateDate': this.dataForm.updateDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
